package dblp.merge;

import java.util.*;

import domain.Author;
import domain.AuthorPair;
import domain.Seed;
import domain.SeedStatus;
import repositories.AuthorRepository;
import repositories.SeedRepository;

public class DBLPMerge {

	private AuthorRepository authorRepository;
	private SeedRepository seedRepository;

	public static void main(String[] args) {
		DBLPMerge merge = new DBLPMerge();
		try {
			merge.mergeAuthors();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	public DBLPMerge() {
		this.authorRepository = new AuthorRepository();
		this.seedRepository = new SeedRepository();
	}
	
	private void mergeAuthorPair(String authorName, String coAuthorName) {
		
		List<UUID> authorIdentifiers = authorRepository.getAuthorIdentifiersForCoAuthors(authorName, coAuthorName);
		if (authorIdentifiers.size() > 1) {
		
		UUID primaryID = authorIdentifiers.get(0);
		authorIdentifiers.remove(0);
		try {
			authorRepository.mergeAuthorIdentifiers(primaryID, authorIdentifiers);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		}
	}


	public void mergeAuthors() throws Exception {
		
		String[] pair = authorRepository.getHighestOrderPair();

		while (pair != null) {
			System.out.println(String.format("Merging based on author pair'%s/%s'.", pair[0], pair[1]));
		
			mergeAuthorPair(pair[0], pair[1]);
			mergeAuthorPair(pair[1], pair[0]);
			
			pair = authorRepository.getHighestOrderPair();
		}
		
		/*
		//Seed seed = seedRepository.getCoAuthorSeed();

		//seed.setStatus(SeedStatus.MergingCoAuthors);
		//seedRepository.saveSeed(seed);

		//System.out.println(String.format("Processing '%s'", seed.getSeed()));

		boolean hasMerges = false;
		int start = 0;

		do {
			
			
			Vector<Author> authors = authorRepository.getAuthors("jalal kawash");
			
			System.out.println(String.format("Found %d authors.", authors.size()));
			
			if (authors.size() == 1)
				break;
			
			hasMerges = false;
			for(int i = 0; i < authors.size(); i++) {
				Author authorA = authors.get(i);
				System.out.println(String.format("\n\n_Author A_ - CoAuthors: %d %s", i, authorA.getCoAuthorNames()));
				for(int j = i + 1; j < authors.size(); j++) {
					Author authorB = authors.get(j);
					System.out.println(String.format("Author B - CoAuthors: %d %s", j, authorB.getCoAuthorNames()));
					if (authorA.getAuthorID() != authorB.getAuthorID()) {
						Vector<AuthorPair> pairs = (Vector<AuthorPair>) Author.getCoAuthorPairs(authorA, authorB);
						if (pairs != null && pairs.size() > 0) {
							hasMerges = true;
							System.out.println(String.format("Merging '%s'", authorA.getFullName()));
							authorRepository.mergeAuthors(authorA, authorB);

							System.out.println(String.format("    New AuthorID: A '%s' / B '%s'", authorA.getAuthorID().toString(), authorB.getAuthorID().toString()));
							
							for(AuthorPair pair : pairs) {
								System.out.println(String.format("Merging '%s'", pair.getAuthorA().getFullName()));

								authorRepository.mergeAuthors(pair.getAuthorA(), pair.getAuthorB());
								
								System.out.println(String.format("    New AuthorID: A '%s' / B '%s'", pair.getAuthorA().getAuthorID().toString(), pair.getAuthorB().getAuthorID().toString()));
								break;
								
							}
						}
					}
				}
			}
		} while (hasMerges);

		//seed.setStatus(SeedStatus.MergedCoAuthors);
		//seedRepository.saveSeed(seed);
		 * 
		 */
	}
}
